package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.domain.ChargingStation;
import com.bwie.common.domain.Collect;
import com.bwie.common.domain.Vehicle;
import com.bwie.common.domain.request.ChargingRequest;
import com.bwie.common.redis.RedisCache;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.common.utils.FastUtil;
import com.bwie.mapper.ChargingMapper;
import com.bwie.service.ChargingService;
import com.bwie.service.CollectService;
import com.bwie.service.VehicleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author：ShenJiaFeng
 * @Package：com.bwie.service.impl
 * @Project：zg6_yuekao
 * @name：ChargingServiceImpl
 * @Date：2024/5/17 12:35
 */
@Service
public class ChargingServiceImpl extends ServiceImpl<ChargingMapper, ChargingStation>
        implements ChargingService {
    @Autowired
    private ChargingMapper chargingMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private FastUtil fastUtil;
    @Autowired
    private CollectService collectService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private HttpServletRequest request;

    @Override
    public PageResult<ChargingStation> chargingList(ChargingRequest chargingRequest) {
        PageHelper.startPage(chargingRequest.getPageNum(), chargingRequest.getPageSize());
        List<ChargingStation> chargingStations = chargingMapper.chargingList(chargingRequest);
        PageInfo<ChargingStation> info = new PageInfo<>(chargingStations);
        return PageResult.toPageResult(info.getTotal(), chargingStations);

    }

    @Override
    public void updateType(ChargingStation chargingStation) {
        if (chargingStation == null) {
            throw new IllegalArgumentException("没有该数据");
        }
        this.updateById(chargingStation);
        redisCache.setCacheObject("type", chargingStation.getTypeId());
    }

    @Override
    public Result fastUpload(MultipartFile multipartFile) {
        String upload = "";
        try {
            upload = fastUtil.upload(multipartFile);
            if (upload.length() <= 0) {
                throw new IllegalArgumentException("上传失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return Result.success("http://124.222.71.202:8888/" + upload, "上传成功");
    }

    @Override
    public void addCollect(Long chargingId) {
        ChargingStation chargingStation = this.getById(chargingId);
        if (chargingStation == null) {
            throw new IllegalArgumentException("当前数据为空");
        }
        List<Collect> collectList = collectService.list();
        if (!collectList.isEmpty()) {
            for (Collect collect1 : collectList) {
                if (collect1.getChargingId().equals(chargingId)) {

                }
            }
        }
        Collect collect = new Collect();
        //默认为1
        collect.setCollectNum(1);
        collect.setChargingId(chargingStation.getChargingId());
        collectService.save(collect);
    }

    @Override
    public void updStatus(Long chargingId) {
        chargingMapper.updStatus(chargingId);
    }

    @Override
    public void updStatusTwo(Long chargingId) {
        chargingMapper.updStatusTwo(chargingId);
    }

    @Override
    public void addVehicle(Vehicle vehicle) {
        matchesParam(vehicle);
        vehicleService.save(vehicle);
    }

    //默认状态
    @Override
    public void updateVehicleStatus(Vehicle vehicle) {
        chargingMapper.updateVehicleStatus(vehicle);
        List<Vehicle> list = vehicleService.list();
        for (Vehicle vehicle1 : list) {
            redisCache.setCacheObject("vehicle",vehicle1);
        }

    }

    @Override
    public void updateVehicleStatusTwo(Vehicle vehicle) {
        List<Vehicle> list = vehicleService.list();
        for (Vehicle vehicle1 : list) {
            redisCache.setCacheObject("vehicle",vehicle1);
        }
        chargingMapper.updateVehicleStatusTwo(vehicle);
    }


    private void matchesParam(Vehicle vehicle) {
        if (vehicle.getLicensePlate() == null){
            throw new IllegalArgumentException("车辆不能为空!");
        }
        //存入redis中
        redisCache.setCacheObject("vehicle",vehicle);
        vehicle.setUserId(1L);
    }

}
